/*package com.orkhonsoft.tengrinshar.chat.config;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.orkhonsoft.tengrinshar.chat.entity.ChatRecordEntity;
import com.orkhonsoft.tengrinshar.chat.entity.FriendApplyEntity;
import com.orkhonsoft.tengrinshar.chat.entity.UserEntity;
import com.orkhonsoft.tengrinshar.chat.mapper.UserMapper;
import com.orkhonsoft.tengrinshar.chat.service.ChatRecordService;
import com.orkhonsoft.tengrinshar.chat.service.FileService;



@Component
public class MessageEventHandler 
{
	private final SocketIOServer server;
	private static int onlineCount = 0;
//	@Autowired
//	private ClientInfoRepository clientInfoRepository;
	@Autowired
	private UserMapper userMapper;
	@Value("${upload.path}")
	private  String path;
	@Autowired
	private FileService fileService;
	@Autowired
	private ChatRecordService ChatRecordService;
	@Autowired
    public MessageEventHandler(SocketIOServer server) 
	{
        this.server = server;
    }
	//添加connect事件，当客户端发起连接时调用，本文中将clientid与sessionid存入数据库
	//方便后面发送消息时查找到对应的目标client,
	@SuppressWarnings("null")
	@OnConnect
    public void onConnect(SocketIOClient client)
    {
		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
		//ClientInfo clientInfo = clientInfoRepository.findClientByclientid(Long.parseLong(clientId));
		UserEntity user=userMapper.queryObject(Long.parseLong(clientId));
		//UUID uuid = new UUID(clientInfo.getMostsignbits(), clientInfo.getLeastsignbits());
		//System.out.println(uuid.toString());
		//System.out.println(client.getSessionId());
		Date nowTime = new Date(System.currentTimeMillis());
		user.setConnected((short)1);
		user.setMostsignbits(client.getSessionId().getMostSignificantBits());
		user.setLeastsignbits(client.getSessionId().getLeastSignificantBits());
			//clientInfo.setLastconnecteddate(nowTime);
//			clientInfo.setSessionid(client.getSessionId().toString());
		userMapper.update(user);
		addOnlineCount();           //在线数加1
	    System.out.println("有新连接加入！当前在线人数为" + getOnlineCount());
	    List<ChatRecordEntity> list=   ChatRecordService.getChatRecordEntitys(Long.parseLong(clientId));
	    if(list.size()>0){
	    	 ChatRecordService.BantchUpdate(list);
	    }
	    client.sendEvent("messageevent", list);
	   
	     
    }
	
	//添加@OnDisconnect事件，客户端断开连接时调用，刷新客户端信息
	@OnDisconnect
	public void onDisconnect(SocketIOClient client)
	{
		String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
//		ClientInfo clientInfo = clientInfoRepository.findClientByclientid(Long.parseLong(clientId));
		UserEntity user=userMapper.queryObject(Long.parseLong(clientId));
		user.setConnected((short)0);
		userMapper.update(user);
		subOnlineCount();           //在线数减1
	    System.out.println("有一连接关闭！当前在线人数为" + getOnlineCount());
	}
	
	//消息接收入口，当接收到消息后，查找发送目标客户端，并且向该客户端发送消息，且给自己发送消息
	@OnEvent(value = "messageevent")
    public void onEvent(SocketIOClient client, AckRequest request, @RequestBody ChatRecordEntity data) throws IllegalStateException, IOException 
	{
//		Gson gson = new Gson();
//		ChatRecordEntity yActivity1 = gson.fromJson(data, new TypeToken<ChatRecordEntity>() {
//		}.getType());
		Long targetClientId = data.getReceiveUserId();
//		ClientInfo clientInfo = clientInfoRepository.findClientByclientid(targetClientId);
		UserEntity user=userMapper.queryObject(targetClientId);
		if (user.getConnected() != 0)
		{
        	UUID uuid = new UUID(user.getMostsignbits(), user.getLeastsignbits());
//			System.out.println(uuid.toString());
//			MessageInfo sendData = new MessageInfo();
//			sendData.setSourceClientId(data.);
//			sendData.setTargetClientId(data.getTargetClientId());
//			sendData.setMsgType("chat");
//			sendData.setMsgContent(data.getMsgContent());
			//type=0 文字,type=1语音文件，type=2其他文件
			long time = System.currentTimeMillis();  
			client.sendEvent("messageevent", data);
			data.setIsRead(0);
			ChatRecordService.save(data);
			System.out.println(client.getSessionId());
			server.getClient(uuid).sendEvent("messageevent", data);
			
//			else if("1".equals(type)){
//				File filePath = new File(path);
//				String fileurl=time+file.getOriginalFilename();
//				if (null != file && file.getSize() > 0) {
//					filePath.mkdirs();
//					filePath = new File(filePath+File.separator+fileurl);
//					file.transferTo(filePath);
//					}
//				FileEntity fileEntity=new FileEntity();
//				fileEntity.setCreateTime(new Date());
//				fileEntity.setExt(file.getContentType());
//				fileEntity.setHash(file.getName());
//				fileEntity.setName(file.getOriginalFilename());
//				fileEntity.setPath(path+File.separator+fileurl);
//				fileEntity.setSize(file.getSize());
//				fileEntity.setType(0);
//				fileService.save(fileEntity);
//				data.setFileId(fileEntity.getId());
//				data.setContent(fileEntity.getPath());
//				ChatRecordService.save(data);
//			}
//			else{
//				File filePath = new File(path);
//				String fileurl=time+file.getOriginalFilename();
//				if (null != file && file.getSize() > 0) {
//					filePath.mkdirs();
//					filePath = new File(filePath+File.separator+fileurl);
//					file.transferTo(filePath);
//					}
//				FileEntity fileEntity=new FileEntity();
//				fileEntity.setCreateTime(new Date());
//				fileEntity.setExt(file.getContentType());
//				fileEntity.setHash(file.getName());
//				fileEntity.setName(file.getOriginalFilename());
//				fileEntity.setPath(path+File.separator+fileurl);
//				fileEntity.setSize(file.getSize());
//				fileEntity.setExtra(extra);
//				fileService.save(fileEntity);
//				data.setFileId(fileEntity.getId());
//				data.setContent(fileEntity.getPath());
//				ChatRecordService.save(data);
//			}
			
		}
		*//**
		 * 该客户端已经退出socket 通过极光方式推送消息
		 *//*
		else{
			 data.setIsRead(1);//推送标记
			 ChatRecordService.save(data);
		}
    }
	
	
	public static synchronized int getOnlineCount() {
        return onlineCount;
    }
	public static synchronized void addOnlineCount() {
		MessageEventHandler.onlineCount++;
    }

    public static synchronized void subOnlineCount() {
    	MessageEventHandler.onlineCount--;
    }
}
*/